home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / folded_u.zip / EXAMPLE.ASM < prev    next >
Assembly Source File  |  1991-03-04  |  10KB  |  265 lines

  1. ;EXECWIN.ASM
  2. ;Written by Kim Kokkonen, TurboPower Software
  3. ;10/88
  4. ;Released to the public domain
  5.  
  6. ;@/// equates ;
  7. col        equ     (byte ptr 0)
  8. row        equ     (byte ptr 1)
  9.  
  10. ofst       equ     (word ptr 0)
  11. segm       equ     (word ptr 2)
  12. ;@\\\;
  13.  
  14. ;@/// ToDos      macro                        ;Transfer control to DOS ;
  15. ToDos      macro                        ;Transfer control to DOS
  16.            jmp  dword ptr Int21CS
  17.            endm
  18. ;@\\\0002000141000141;
  19. ;@/// ToApp      macro                        ;Transfer control back to caller ;
  20. ToApp      macro                        ;Transfer control back to caller
  21.            clc                          ;Clear error flag
  22.            ret     2                    ;Return with flags intact
  23.            endm
  24. ;@\\\0000000149;
  25.  
  26. ;@/// data       segment ;
  27. data       segment word public
  28.            extrn   SaveInt21 : dword    ;Previous $21 vector
  29.            extrn   WindPos   : word     ;Cursor position in window
  30.            extrn   WindLo    : word     ;Top left corner of window
  31.            extrn   WindHi    : word     ;   and bottom right corner.
  32.            extrn   WindAttr  : byte     ;Attribute with which to
  33.                                         ;   display all characters.
  34. data       ends
  35. ;@\\\;
  36. ;@/// code       segment ;
  37. code       segment byte public
  38.            assume  cs:code,ds:data,es:nothing
  39.            public  SetCsInts
  40.            public  NewInt21
  41.  
  42. Int21CS    dd      ?            ;Old interrupt 21 in code segment
  43.  
  44. ;@/// SetCsInts  proc    near         ; Save ints in code segment ;
  45. SetCsInts  proc    near
  46. ; Save ints in code segment
  47.            les     ax,SaveInt21
  48.            mov     Int21CS.ofst,ax
  49.            mov     Int21CS.segm,es
  50.            ret
  51. SetCsInts  endp
  52. ;@\\\0000000201;
  53. ;@/// NewInt21   proc    far          ; Handle interrupt 21 to trap output calls ;
  54. NewInt21   proc    far
  55. ; Handle interrupt 21 to trap output calls
  56.  
  57.            cmp     ah,2                 ;Just get functions that go to StdOut
  58.            jz      DispOut
  59.            cmp     ah,6
  60.            jz      DirectOut
  61.            cmp     ah,9
  62.            jz      StringOut
  63.            cmp     ah,40h               ;Or maybe to StdErr
  64.            jz      BlockOut
  65.            ToDos
  66.  
  67. ;@/// DispOut:                                ;DOS function 2 ;
  68. DispOut:                                ;DOS function 2
  69.            push    ax
  70.            mov     al,dl                ;Character to write in AL
  71.            call    WriteChar            ;Write via video BIOS
  72.            pop     ax
  73.            ToApp                        ;Return successfully
  74. ;@\\\0000000138;
  75. ;@/// DirectOut:                              ;DOS function 6 ;
  76. DirectOut:                              ;DOS function 6
  77.            cmp     dl,0FFh              ;Console input?
  78.            jnz     DispOut              ;Jump if not
  79.            ToDos                        ;Else transfer to DOS
  80. ;@\\\0000000138;
  81. ;@/// StringOut:                              ;DOS function 9 ;
  82. StringOut:                              ;DOS function 9
  83.            push    ax                   ;Save AX
  84.            push    bx                   ;Save string index
  85.            mov     bx,dx                ;DS:BX -> string
  86. StringOut1:
  87.            mov     al,[bx]              ;AL = next character to write
  88.            cmp     al,'$'               ;Terminator?
  89.            jz      StringOut2           ;Exit if so
  90.            call    WriteChar            ;Write it
  91.            inc     bx                   ;Next character
  92.            jmp     StringOut1           ;Loop
  93. StringOut2:
  94.            pop     bx
  95.            pop     ax
  96.            ToApp                        ;Back to application
  97. ;@\\\0000000138;
  98. ;@/// BlockOut:                               ;DOS function 40h ;
  99. BlockOut:                               ;DOS function 40h
  100.            cmp     bx,1                 ;To StdOut?
  101.            jz      BlockOut1            ;Jump if so
  102.            cmp     bx,2                 ;To StdErr?
  103.            jz      BlockOut1            ;Jump if so
  104.            ToDos                        ;Else let DOS handle it
  105. BlockOut1:
  106.            jcxz    BlockOut3            ;Get out if none to write
  107.            push    ax
  108.            push    bx
  109.            push    cx                   ;Save loop counter
  110.            mov     bx,dx                ;DS:BX -> stuff to write
  111. BlockOut2:
  112.            mov     al,[bx]              ;Next character to write
  113.            call    WriteChar            ;Write it
  114.            inc     bx                   ;Next index
  115.            loop    BlockOut2            ;Loop for all the characters
  116.            pop     cx
  117.            pop     bx
  118.            pop     ax
  119.            mov     ax,cx                ;Wrote all the characters
  120. BlockOut3:
  121.            ToApp                        ;Back to application
  122. ;@\\\;
  123. NewInt21 endp
  124. ;@\\\0000000201;
  125. ;@/// WriteChar  proc    near         ; Write a character via BIOS ;
  126. WriteChar  proc    near
  127. ; Write a character to current position via BIOS
  128. ; Entry: AL is character to write
  129. ; Must preserve all but AX
  130. ;@///            preserve registers bp,bx,cx,dx,ds ;
  131.            push    bp                   ;some versions of int 10 BIOS trash BP
  132.            push    bx
  133.            push    cx
  134.            push    dx
  135.            push    ds
  136. ;@\\\;
  137.            mov     bx,seg data          ;set up ds
  138.            mov     ds,bx
  139.  
  140.            cmp     al,7                 ;Bell character?
  141.            jz      BiosWriteDone        ;Don't write
  142.  
  143.            mov     dx,WindPos           ;Current cursor pos in DX
  144. ;@///            special characters? ;
  145.            cmp     al,8                 ;Backspace?
  146.            jz      BackSpace
  147.            cmp     al,9                 ;Tab?
  148.            jz      Tab
  149.            cmp     al,10                ;Line feed?
  150.            jz      LineFeed
  151.            cmp     al,13                ;Carriage return?
  152.            jz      Carriage
  153. ;@\\\;
  154.            call    WriteOne             ;Write one normal character
  155.  
  156. ;@/// BiosSetCursor: ;
  157. BiosSetCursor:                          ;Position cursor
  158.            xor     bh,bh
  159.            mov     ah,2
  160.            int     10h
  161.            mov     WindPos,dx           ;Save new cursor position
  162. ;@\\\0000000139;
  163. ;@/// BiosWriteDone: ;
  164. BiosWriteDone:
  165.            pop     ds
  166.            pop     dx
  167.            pop     cx
  168.            pop     bx
  169.            pop     bp
  170.            ret
  171. ;@\\\000000010F;
  172. ;@/// Carriage: ;
  173. Carriage:  mov     dl,WindLo.col        ;Move to left edge
  174.            jmp     BiosSetCursor
  175. ;@\\\000000013B;
  176. ;@/// LineFeed: ;
  177. LineFeed:  cmp     dh,WindHi.row        ;Room to increment row?
  178.            jb      LineFeed1
  179.            mov     ax,0601h             ;Scroll up one line
  180.            mov     cx,WindLo
  181.            mov     dx,WindHi
  182.            mov     bh,WindAttr
  183.            int     10h
  184.            jmp     BiosWriteDone
  185. LineFeed1: inc     dh                   ;Increment row
  186.            jmp     BiosSetCursor        ;Set cursor
  187. ;@\\\0000000140;
  188. ;@/// Tab: ;
  189. Tab:       mov     cl,dl
  190.            sub     cl,WindLo.Col        ;Characters beyond left edge
  191.            add     cl,8
  192.            and     cl,0F8h              ;To next tab stop
  193.            add     cl,WindLo.Col        ;Window coords
  194.            sub     cl,dl                ;Spaces to write
  195.            xor     ch,ch                ;CX = spaces to write
  196. Tab1:      mov     al,20h               ;Write spaces
  197.            push    cx
  198.            call    WriteOne             ;One at a time
  199.            xor     bh,bh
  200.            mov     ah,2
  201.            int     10h
  202.            mov     WindPos,dx           ;Save new cursor position
  203.            pop     cx
  204.            loop    Tab1                 ;Do all of them
  205.            jmp     BiosWriteDone
  206. ;@\\\0000000119;
  207. ;@/// BackSpace: ;
  208. BackSpace: cmp     dl,WindLo.col        ;Beyond left edge?
  209.            jbe     BiosWriteDone        ;Exit if not
  210.            dec     dl                   ;One left
  211.            xor     bh,bh
  212.            mov     ah,2                 ;Position cursor
  213.            int     10h
  214.            mov     WindPos,dx
  215.            mov     cx,1                 ;Write character
  216.            mov     bl,WindAttr
  217.            mov     ax,0920h             ;Write a space
  218.            int     10h
  219.            jmp     BiosWriteDone        ;Done now
  220. ;@\\\000000013B;
  221.  
  222. WriteChar  endp
  223. ;@\\\0000000201;
  224. ;@/// WriteOne   proc    near         ; Write one character and update cursor ;
  225. WriteOne   proc    near
  226. ; Write one character and update cursor variable
  227.  
  228. ;@///            write character ;
  229.            mov     cx,1
  230.            mov     bl,WindAttr
  231.            xor     bh,bh
  232.            mov     ah,9
  233.            int     10h
  234. ;@\\\;
  235.  
  236.            cmp     dl,WindHi.col        ;Below right border?
  237.            jb      IncCol               ;If so, just increment column
  238.            cmp     dh,WindHi.row        ;Room for CR/LF?
  239.            jb      IncRow               ;Jump if so
  240.  
  241. ;@///            scroll up one line ;
  242.            mov     ax,0601h
  243.            mov     cx,WindLo
  244.            mov     dx,WindHi
  245.            mov     bh,WindAttr
  246.            int     10h
  247.            dec     dh                   ;Compensate for inc to follow
  248. ;@\\\000C000129000129000129;
  249.  
  250. IncRow:    inc     dh                   ;Next row
  251.            mov     dl,WindLo.col        ;First col
  252.            dec     dl                   ;Compensate for inc to follow
  253.  
  254. IncCol:    inc     dl                   ;Increment column
  255.            ret
  256.  
  257. WriteOne   endp
  258. ;@\\\0030000B0C000B1E000201;
  259.  
  260. code       ends
  261. ;@\\\;
  262.  
  263.            end
  264. ;@\\\0001000009000801;
  265.